﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Reflection;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class EquipmentManager
    {
        private Character MyCharacter;

        private Object equipmentLock = new Object();

        public int WaterMotion()
        {
            lock (equipmentLock)
            {
                if (_Feet == null)
                    return -1;

                return _Feet.WaterMotion;
            }
        }

        public bool MeetsRequirements(Equipment equip)
        {
            if (equip == null)
                return false;

            lock (equipmentLock)
            {
                //Check if the item is bound to someone else
                if (equip.BoundTo > 0 && equip.BoundTo != MyCharacter.ID)
                    return false;

                //Make sure they have all the mandatory requirements
                foreach (LevelRequirement lr in equip.RequirementsAll)
                {
                    if (lr.Skill == "Any")
                    {
                        if (lr.LevelRequired > MyCharacter.skillManager.getMaxSkill())
                            return false;
                    }
                    else
                    {
                        int currentSkillLevel = System.Convert.ToInt32(Math.Floor(MyCharacter.skillManager.GetCurrentLevel(lr.Skill)));
                        if (currentSkillLevel < lr.LevelRequired)
                            return false;
                    }
                }
                //If no other requirements return true
                if (equip.RequirementsAny.Count == 0)
                    return true;

                //Check if they have at least 1 of the optional requirements
                foreach (LevelRequirement lr in equip.RequirementsAny)
                {
                    if (lr.Skill == "Any")
                    {
                        if (MyCharacter.skillManager.getMaxSkill() >= lr.LevelRequired)
                            return true;
                    }
                    else
                    {
                        int currentSkillLevel = System.Convert.ToInt32(Math.Floor(MyCharacter.skillManager.GetCurrentLevel(lr.Skill)));
                        if (currentSkillLevel >= lr.LevelRequired)
                            return true;
                    }
                }

                //We checked for all optionals and none were found, return false
                return false;
            }
        }

        public Equipment RemoveEquipment(string slot)
        {
            lock (equipmentLock)
            {
                FieldInfo fi = this.GetType().GetField("_" + slot, BindingFlags.Instance | BindingFlags.NonPublic);
                if (fi.GetValue(this) == null)
                    return null;
                
                Equipment ItemToRemove = (Equipment)fi.GetValue(this);
                fi.SetValue(this, null);

                MyCharacter.Armor = MyCharacter.Armor - ItemToRemove.Armor;
                MyCharacter.AgilityAdd = MyCharacter.AgilityAdd - ItemToRemove.Agility;
                MyCharacter.DexterityAdd = MyCharacter.DexterityAdd - ItemToRemove.Dexterity;
                MyCharacter.IntelligenceAdd = MyCharacter.IntelligenceAdd - ItemToRemove.Intelligence;
                MyCharacter.StrengthAdd = MyCharacter.StrengthAdd - ItemToRemove.Strength;
                MyCharacter.WisdomAdd = MyCharacter.WisdomAdd - ItemToRemove.Wisdom;

                MyCharacter.HPRegen = MyCharacter.HPRegen - ItemToRemove.HP_Regen;

                MyCharacter.MaxHPAdd = MyCharacter.MaxHPAdd - ItemToRemove.HP;
                if (MyCharacter.HP > MyCharacter.MaxHP)
                    MyCharacter.HP = MyCharacter.MaxHP;

                MyCharacter.StaminaRegen = MyCharacter.StaminaRegen - ItemToRemove.Stam_Regen;

                MyCharacter.MaxStaminaAdd = MyCharacter.MaxStaminaAdd - ItemToRemove.Stam;
                if (MyCharacter.Stamina > MyCharacter.MaxStamina)
                    MyCharacter.Stamina = MyCharacter.MaxStamina;

                MyCharacter.ManaRegen = MyCharacter.ManaRegen - ItemToRemove.Mana_Regen;

                MyCharacter.MaxManaAdd = MyCharacter.MaxManaAdd - ItemToRemove.Mana;
                if (MyCharacter.Mana > MyCharacter.MaxMana)
                    MyCharacter.Mana = MyCharacter.MaxMana;

                MyCharacter.ResistFire = MyCharacter.ResistFire - ItemToRemove.ResistFire;
                MyCharacter.ResistIce = MyCharacter.ResistIce - ItemToRemove.ResistIce;
                MyCharacter.ResistHoly = MyCharacter.ResistHoly - ItemToRemove.ResistHoly;
                MyCharacter.ResistUnholy = MyCharacter.ResistUnholy - ItemToRemove.ResistUnholy;
                MyCharacter.ResistMagic = MyCharacter.ResistMagic - ItemToRemove.ResistMagic;
                MyCharacter.ResistPhysical = MyCharacter.ResistPhysical - ItemToRemove.ResistPhysical;

                return ItemToRemove;
            }
        }

        public bool AddEquipment(Equipment equip, string slot)
        {
            lock (equipmentLock)
            {
                //Use reflection to set the variables above because its not as annoying as a huge switch to translate string parameter to variable
                FieldInfo fi = this.GetType().GetField("_" + slot, BindingFlags.Instance | BindingFlags.NonPublic);
                if (fi.GetValue(this) != null)
                    return false;

                //Check if the item BoundTo is < -1, this means the character has just logged in and is wearing a bindable item, meaning it must be bound to them
                if (equip.BoundTo < -1)
                    equip.BoundTo = MyCharacter.ID;

                fi.SetValue(this, equip);

                MyCharacter.Armor = MyCharacter.Armor + equip.Armor;
                MyCharacter.AgilityAdd = MyCharacter.AgilityAdd + equip.Agility;
                MyCharacter.DexterityAdd = MyCharacter.DexterityAdd + equip.Dexterity;
                MyCharacter.IntelligenceAdd = MyCharacter.IntelligenceAdd + equip.Intelligence;
                MyCharacter.StrengthAdd = MyCharacter.StrengthAdd + equip.Strength;
                MyCharacter.WisdomAdd = MyCharacter.WisdomAdd + equip.Wisdom;

                MyCharacter.HPRegen = MyCharacter.HPRegen + equip.HP_Regen;
                MyCharacter.MaxHPAdd = MyCharacter.MaxHPAdd + equip.HP;
                MyCharacter.StaminaRegen = MyCharacter.StaminaRegen + equip.Stam_Regen;
                MyCharacter.MaxStaminaAdd = MyCharacter.MaxStaminaAdd + equip.Stam;
                MyCharacter.ManaRegen = MyCharacter.ManaRegen + equip.Mana_Regen;
                MyCharacter.MaxManaAdd = MyCharacter.MaxManaAdd + equip.Mana;

                MyCharacter.ResistFire = MyCharacter.ResistFire + equip.ResistFire;
                MyCharacter.ResistIce = MyCharacter.ResistIce + equip.ResistIce;
                MyCharacter.ResistHoly = MyCharacter.ResistHoly + equip.ResistHoly;
                MyCharacter.ResistUnholy = MyCharacter.ResistUnholy + equip.ResistUnholy;
                MyCharacter.ResistMagic = MyCharacter.ResistMagic + equip.ResistMagic;
                MyCharacter.ResistPhysical = MyCharacter.ResistPhysical + equip.ResistPhysical;

                return true;
            }
        }

        public Equipment GetEquipment(string slot)
        {
            lock (equipmentLock)
            {
                FieldInfo fi = this.GetType().GetField("_" + slot, BindingFlags.Instance | BindingFlags.NonPublic);

                if (fi.GetValue(this) == null)
                    return null;

                else
                    return (Equipment)fi.GetValue(this);
            }
        }

        private Equipment _EarLeft = null;
        private Equipment _EarRight = null;
        private Equipment _Neck = null;
        private Equipment _Head = null;
        private Equipment _Face = null;
        private Equipment _WristLeft = null;
        private Equipment _WristRight = null;
        private Equipment _Shoulders = null;
        private Equipment _Chest = null;
        private Equipment _Back = null;
        private Equipment _Arms = null;
        private Equipment _Hands = null;
        private Equipment _Feet = null;
        private Equipment _Legs = null;

        private Equipment _RingLeft1 = null;
        private Equipment _RingLeft2 = null;
        private Equipment _RingLeft3 = null;
        private Equipment _RingLeft4 = null;

        private Equipment _RingRight1 = null;
        private Equipment _RingRight2 = null;
        private Equipment _RingRight3 = null;
        private Equipment _RingRight4 = null;

        public EquipmentManager(Character myCharacter)
        {
            MyCharacter = myCharacter;
            
            //LOAD ITEM IDs
            SqlConnection MyCon = new SqlConnection(Managers.DBConnectionString);
            SqlCommand SQLCom = new SqlCommand("Characters_Equipment_Select", MyCon);
            SQLCom.CommandType = System.Data.CommandType.StoredProcedure;
            SQLCom.Parameters.AddWithValue("@CharacterID", MyCharacter.ID);

            MyCon.Open();
            SqlDataReader SDR = SQLCom.ExecuteReader();

            while (SDR.Read())
            {
                if (SDR["EarLeft"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["EarLeft"].ToString())), "EarLeft");
                if (SDR["EarRight"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["EarRight"].ToString())), "EarRight");
                if (SDR["Neck"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Neck"].ToString())), "Neck");
                if (SDR["Head"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Head"].ToString())), "Head");
                if (SDR["Face"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Face"].ToString())), "Face");
                if (SDR["WristLeft"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["WristLeft"].ToString())), "WristLeft");
                if (SDR["WristRight"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["WristRight"].ToString())), "WristRight");
                if (SDR["Shoulders"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Shoulders"].ToString())), "Shoulders");
                if (SDR["Chest"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Chest"].ToString())), "Chest");
                if (SDR["Back"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Back"].ToString())), "Back");

                if (SDR["Arms"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Arms"].ToString())), "Arms");
                if (SDR["Hands"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Hands"].ToString())), "Hands");
                if (SDR["Feet"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Feet"].ToString())), "Feet");
                if (SDR["Legs"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["Legs"].ToString())), "Legs");

                if (SDR["RingLeft1"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["RingLeft1"].ToString())), "RingLeft1");
                if (SDR["RingLeft2"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["RingLeft2"].ToString())), "RingLeft2");
                if (SDR["RingLeft3"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["RingLeft3"].ToString())), "RingLeft3");
                if (SDR["RingLeft4"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["RingLeft4"].ToString())), "RingLeft4");

                if (SDR["RingRight1"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["RingRight1"].ToString())), "RingRight1");
                if (SDR["RingRight2"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["RingRight2"].ToString())), "RingRight2");
                if (SDR["RingRight3"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["RingRight3"].ToString())), "RingRight3");
                if (SDR["RingRight4"] != DBNull.Value)
                    AddEquipment((Equipment)MyCharacter.GameHandler.CreateItem(int.Parse(SDR["RingRight4"].ToString())), "RingRight4");
            }

            SDR.Close();
            MyCon.Close();
            MyCon.Dispose();
        }

        public bool isValidSlot(string slot)
        {
            FieldInfo[] fi = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (FieldInfo fieldInfo in fi)
            {
                if (fieldInfo.Name == "_" + slot)
                    return true;
            }

            return false;
        }

        public bool hasItem(int itemId)
        {
            lock (equipmentLock)
            {
                if (_EarLeft != null && _EarLeft.ID == itemId)
                    return true;
                if (_EarRight != null && _EarRight.ID == itemId)
                    return true;
                if (_Neck != null && _Neck.ID == itemId)
                    return true;
                if (_Head != null && _Head.ID == itemId)
                    return true;
                if (_Face != null && _Face.ID == itemId)
                    return true;
                if (_WristLeft != null && _WristLeft.ID == itemId)
                    return true;
                if (_WristRight != null && _WristRight.ID == itemId)
                    return true;
                if (_Shoulders != null && _Shoulders.ID == itemId)
                    return true;
                if (_Chest != null && _Chest.ID == itemId)
                    return true;
                if (_Back != null && _Back.ID == itemId)
                    return true;
                if (_Arms != null && _Arms.ID == itemId)
                    return true;
                if (_Hands != null && _Hands.ID == itemId)
                    return true;
                if (_Feet != null && _Feet.ID == itemId)
                    return true;
                if (_Legs != null && _Legs.ID == itemId)
                    return true;


                if (_RingLeft1 != null && _RingLeft1.ID == itemId)
                    return true;
                if (_RingLeft2 != null && _RingLeft2.ID == itemId)
                    return true;
                if (_RingLeft3 != null && _RingLeft3.ID == itemId)
                    return true;
                if (_RingLeft4 != null && _RingLeft4.ID == itemId)
                    return true;

                if (_RingRight1 != null && _RingRight1.ID == itemId)
                    return true;
                if (_RingRight2 != null && _RingRight2.ID == itemId)
                    return true;
                if (_RingRight3 != null && _RingRight3.ID == itemId)
                    return true;
                if (_RingRight4 != null && _RingRight4.ID == itemId)
                    return true;

                return false;
            }
        }

        public void SaveEquipment()
        {
            SqlConnection MyCon = new SqlConnection(Managers.DBConnectionString);
            SqlCommand SQLCom = new SqlCommand("Characters_Equipment_Update", MyCon);
            SQLCom.CommandType = System.Data.CommandType.StoredProcedure;
            SQLCom.Parameters.AddWithValue("@CharacterID", MyCharacter.ID);

            lock (equipmentLock)
            {
                if (_EarLeft != null)
                    SQLCom.Parameters.AddWithValue("@EarLeft", _EarLeft.ID);
                if (_EarRight != null)
                    SQLCom.Parameters.AddWithValue("@EarRight", _EarRight.ID);
                if (_Neck != null)
                    SQLCom.Parameters.AddWithValue("@Neck", _Neck.ID);
                if (_Head != null)
                    SQLCom.Parameters.AddWithValue("@Head", _Head.ID);
                if (_Face != null)
                    SQLCom.Parameters.AddWithValue("@Face", _Face.ID);

                if (_WristLeft != null)
                    SQLCom.Parameters.AddWithValue("@WristLeft", _WristLeft.ID);
                if (_WristRight != null)
                    SQLCom.Parameters.AddWithValue("@WristRight", _WristRight.ID);
                if (_Shoulders != null)
                    SQLCom.Parameters.AddWithValue("@Shoulders", _Shoulders.ID);
                if (_Chest != null)
                    SQLCom.Parameters.AddWithValue("@Chest", _Chest.ID);
                if (_Back != null)
                    SQLCom.Parameters.AddWithValue("@Back", _Back.ID);

                if (_Arms != null)
                    SQLCom.Parameters.AddWithValue("@Arms", _Arms.ID);
                if (_Hands != null)
                    SQLCom.Parameters.AddWithValue("@Hands", _Hands.ID);
                if (_Feet != null)
                    SQLCom.Parameters.AddWithValue("@Feet", _Feet.ID);
                if (_Legs != null)
                    SQLCom.Parameters.AddWithValue("@Legs", _Legs.ID);

                if (_RingLeft1 != null)
                    SQLCom.Parameters.AddWithValue("@RingLeft1", _RingLeft1.ID);
                if (_RingLeft2 != null)
                    SQLCom.Parameters.AddWithValue("@RingLeft2", _RingLeft2.ID);
                if (_RingLeft3 != null)
                    SQLCom.Parameters.AddWithValue("@RingLeft3", _RingLeft3.ID);
                if (_RingLeft4 != null)
                    SQLCom.Parameters.AddWithValue("@RingLeft4", _RingLeft4.ID);

                if (_RingRight1 != null)
                    SQLCom.Parameters.AddWithValue("@RingRight1", _RingRight1.ID);
                if (_RingRight2 != null)
                    SQLCom.Parameters.AddWithValue("@RingRight2", _RingRight2.ID);
                if (_RingRight3 != null)
                    SQLCom.Parameters.AddWithValue("@RingRight3", _RingRight3.ID);
                if (_RingRight4 != null)
                    SQLCom.Parameters.AddWithValue("@RingRight4", _RingRight4.ID);
            }
            MyCon.Open();
            SQLCom.ExecuteNonQuery();
            MyCon.Close();
            MyCon.Dispose();
        }

        public decimal totalWeight()
        {
            decimal result = 0;

            lock (equipmentLock)
            {
                if (_EarLeft != null)
                    result = result + _EarLeft.Weight;
                if (_EarRight != null)
                    result = result + _EarRight.Weight;
                if (_Neck != null)
                    result = result + _Neck.Weight;
                if (_Head != null)
                    result = result + _Head.Weight;
                if (_Face != null)
                    result = result + _Face.Weight;

                if (_WristLeft != null)
                    result = result + _WristLeft.Weight;
                if (_WristRight != null)
                    result = result + _WristRight.Weight;
                if (_Shoulders != null)
                    result = result + _Shoulders.Weight;
                if (_Chest != null)
                    result = result + _Chest.Weight;
                if (_Back != null)
                    result = result + _Back.Weight;

                if (_Arms != null)
                    result = result + _Arms.Weight;
                if (_Hands != null)
                    result = result + _Hands.Weight;
                if (_Feet != null)
                    result = result + _Feet.Weight;
                if (_Legs != null)
                    result = result + _Legs.Weight;

                if (_RingLeft1 != null)
                    result = result + _RingLeft1.Weight;
                if (_RingLeft2 != null)
                    result = result + _RingLeft2.Weight;
                if (_RingLeft3 != null)
                    result = result + _RingLeft3.Weight;
                if (_RingLeft4 != null)
                    result = result + _RingLeft4.Weight;

                if (_RingRight1 != null)
                    result = result + _RingRight1.Weight;
                if (_RingRight2 != null)
                    result = result + _RingRight2.Weight;
                if (_RingRight3 != null)
                    result = result + _RingRight3.Weight;
                if (_RingRight4 != null)
                    result = result + _RingRight4.Weight;
            }
            return result;
        }

        public string toXML(Character sendingToCharacter)
        {
            string result = "";

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            xmldoc.WriteStartElement("equipment");

            lock (equipmentLock)
            {
                xmldoc.WriteStartElement("EarLeft");
                if (_EarLeft != null)
                    xmldoc.WriteRaw(_EarLeft.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("EarRight");
                if (_EarRight != null)
                    xmldoc.WriteRaw(_EarRight.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Neck");
                if (_Neck != null)
                    xmldoc.WriteRaw(_Neck.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Head");
                if (_Head != null)
                    xmldoc.WriteRaw(_Head.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Face");
                if (_Face != null)
                    xmldoc.WriteRaw(_Face.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("WristLeft");
                if (_WristLeft != null)
                    xmldoc.WriteRaw(_WristLeft.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("WristRight");
                if (_WristRight != null)
                    xmldoc.WriteRaw(_WristRight.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Shoulders");
                if (_Shoulders != null)
                    xmldoc.WriteRaw(_Shoulders.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Chest");
                if (_Chest != null)
                    xmldoc.WriteRaw(_Chest.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Back");
                if (_Back != null)
                    xmldoc.WriteRaw(_Back.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Arms");
                if (_Arms != null)
                    xmldoc.WriteRaw(_Arms.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Hands");
                if (_Hands != null)
                    xmldoc.WriteRaw(_Hands.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Feet");
                if (_Feet != null)
                    xmldoc.WriteRaw(_Feet.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("Legs");
                if (_Legs != null)
                    xmldoc.WriteRaw(_Legs.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("RingLeft1");
                if (_RingLeft1 != null)
                    xmldoc.WriteRaw(_RingLeft1.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("RingLeft2");
                if (_RingLeft2 != null)
                    xmldoc.WriteRaw(_RingLeft2.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("RingLeft3");
                if (_RingLeft3 != null)
                    xmldoc.WriteRaw(_RingLeft3.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("RingLeft4");
                if (_RingLeft4 != null)
                    xmldoc.WriteRaw(_RingLeft4.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("RingRight1");
                if (_RingRight1 != null)
                    xmldoc.WriteRaw(_RingRight1.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("RingRight2");
                if (_RingRight2 != null)
                    xmldoc.WriteRaw(_RingRight2.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("RingRight3");
                if (_RingRight3 != null)
                    xmldoc.WriteRaw(_RingRight3.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();

                xmldoc.WriteStartElement("RingRight4");
                if (_RingRight4 != null)
                    xmldoc.WriteRaw(_RingRight4.toXML(sendingToCharacter));
                xmldoc.WriteEndElement();
            }

            xmldoc.WriteEndElement();
            xmldoc.Flush();

            xmldoc.Close();

            stringWriter.Flush();

            result = stringWriter.ToString();

            return result;
        }
    }
}
